﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Configuration;
using OBUActivation.LOG;

namespace OBUActivation
{

    /// <summary>
    /// 记录数据的工具，具体包括记录命令码，输入以及输出
    /// </summary>
    public class DataRecordUtil
    {
        private static Object _lock = new Object();

        private static FileInfo fileInfo = null;

        private static string DATA_PATH_KEY = "dataPath";

        private DataRecordUtil()
        {
        }


        /// <summary>
        /// 记录数据
        /// </summary>
        /// <param name="command">命令码</param>
        /// <param name="input">输入</param>
        /// <param name="output">输出</param>
        public static void record(String command, String input, String output)
        {

            lock (_lock)
            {
                FileStream fs = getDataFile();
                if (fs != null)
                {
                    try
                    {
                        using (StreamWriter sw = new StreamWriter(fs))
                        {

                            string content = "命令码： " + command + " 输入: " + input + " 输出: " + output;
                            sw.WriteLine(content);

                        }
                    }
                    catch (Exception e)
                    {
                        Log.w("DataRecord record(): " + e.ToString());
                    }

                }

            }

        }


        /// <summary>
        /// 获取data文件
        /// </summary>
        /// <param name="log"></param>
        private static FileStream getDataFile()
        {
            FileStream fs = null;

            try
            {
                DateTime now = DateTime.Now;
                string dataPath = ConfigurationManager.AppSettings[DATA_PATH_KEY];
                string dataName = System.DateTime.Now.ToString("yyyyMMdd") + "data.txt";
                if (dataPath.Equals(""))
                {
                    DirectoryInfo dInfo = new DirectoryInfo("data");
                    if (!dInfo.Exists)
                    {
                        dInfo.Create();
                    }
                    fileInfo = new FileInfo("data" + "\\" + dataName);
                }
                else
                {
                    DirectoryInfo dInfo = new DirectoryInfo(@dataPath);
                    if (!dInfo.Exists)
                    {
                        dInfo.Create();
                    }
                    fileInfo = new FileInfo(@dataPath + "\\" + dataName);
                }

                if (!fileInfo.Exists)
                {
                    fs = new FileStream(fileInfo.FullName, FileMode.CreateNew);
                }
                else
                {
                    fs = new FileStream(fileInfo.FullName, FileMode.Append);
                }



            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

            return fs;
        }


    }
}
